"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DBSourceManager = void 0;
const typeorm_1 = require("typeorm");
const DbConfig_1 = require("../../config/DbConfig");
const Logger_1 = require("./Logger");
class DBSourceManager {
    constructor() {
        this.dataSourceMap = new Map();
    }
    init() {
    }
    createClient(dbName, dbType) {
        return __awaiter(this, void 0, void 0, function* () {
            let realDbName = this.getCodeDbName(dbName, dbType);
            let dbConfig = DbConfig_1.DbConfig.getDbConfig(dbType);
            let entityList = DbConfig_1.DbConfig.getDbEntity(dbName);
            let dataSouce = new typeorm_1.DataSource({
                type: "mysql",
                host: dbConfig.host,
                port: dbConfig.port,
                username: dbConfig.user,
                password: dbConfig.pass,
                database: dbName,
                synchronize: true,
                logging: false,
                subscribers: [],
                migrations: [],
                entities: entityList,
            });
            if (!dataSouce.isInitialized) {
                let initReulst = yield dataSouce.initialize();
            }
            this.dataSourceMap.set(realDbName, dataSouce);
            return dataSouce;
        });
    }
    getCodeDbName(dbName, dbType) {
        let realDbName = dbName + "_db" + dbType;
        return realDbName;
    }
    getDb(dbName, dbType = DbConfig_1.DbConfig.now_db_type) {
        return __awaiter(this, void 0, void 0, function* () {
            let db = null;
            let realDbName = this.getCodeDbName(dbName, dbType);
            if (this.dataSourceMap.has(realDbName)) {
                db = this.dataSourceMap.get(realDbName);
            }
            else {
                db = yield this.createClient(dbName, dbType);
            }
            return db;
        });
    }
    insert(repository, entity) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                let result = yield repository.insert(entity);
                return result;
            }
            catch (error) {
                Logger_1.Logger.error("Repository.insert=", error);
                return null;
            }
        });
    }
    query(repository, sql, sqlParams) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                let results = yield repository.query(sql, sqlParams);
                return results;
            }
            catch (error) {
                Logger_1.Logger.error("Repository.query=", error);
            }
            return [];
        });
    }
}
exports.DBSourceManager = DBSourceManager;
DBSourceManager.instance = new DBSourceManager();
